BIN := lwip_linux_simulator.o

ROOT_DIR := $(abspath $(CURDIR)/..)
PROJ_DIR := ${ROOT_DIR}/simulator
LWIP_DIR := ${ROOT_DIR}/source
BUILD_DIR := ${ROOT_DIR}/simulator/build

# add header files path
INCLUDE_DIRS := -I${LWIP_DIR}/src/include/ipv4
INCLUDE_DIRS += -I${LWIP_DIR}/src/include
INCLUDE_DIRS += -I${LWIP_DIR}/src/include/netif
INCLUDE_DIRS += -I${LWIP_DIR}/src/include/posix
INCLUDE_DIRS += -I${LWIP_DIR}/src/include/posix/sys
INCLUDE_DIRS += -I${LWIP_DIR}/src/netif/ppp

INCLUDE_DIRS += -I${PROJ_DIR}/app
INCLUDE_DIRS += -I${PROJ_DIR}/arch
INCLUDE_DIRS += -I${PROJ_DIR}

# add source files
SOURCE_FILES := $(wildcard ${LWIP_DIR}/src/api/*.c)
SOURCE_FILES += $(wildcard ${LWIP_DIR}/src/core/*.c)
SOURCE_FILES += $(wildcard ${LWIP_DIR}/src/core/ipv4/*.c)
SOURCE_FILES += $(wildcard ${LWIP_DIR}/src/core/snmp/*.c)
SOURCE_FILES += $(wildcard ${LWIP_DIR}/src/netif/*.c)
SOURCE_FILES += $(wildcard ${LWIP_DIR}/src/netif/ppp/*.c)

SOURCE_FILES += $(wildcard ${PROJ_DIR}/arch/*.c)
SOURCE_FILES += $(wildcard ${PROJ_DIR}/app/*.c)
SOURCE_FILES += $(wildcard ${PROJ_DIR}/*.c)

TOOL_PRE :=
CC = ${TOOL_PRE}gcc
AR = ${TOOL_PRE}ar
CFLAGS := -ggdb3 -O0 -g
LDFLAGS := -ggdb3 -O0 -pthread -lpcap -lrt

OBJ_FILES = $(SOURCE_FILES:%.c=$(BUILD_DIR)/%.o)
DEP_FILES = $(OBJ_FILES:%.o=%.d)

${BIN} : $(BUILD_DIR)/$(BIN)

${BUILD_DIR}/${BIN} : ${OBJ_FILES}
	@echo "Build $(notdir $@)..."
	@-mkdir -p ${@D}
	@$(CC) $^ $(CFLAGS) $(INCLUDE_DIRS) ${LDFLAGS} -o $@

-include ${DEP_FILES}

.PHONY: all clean execute
all:
${BUILD_DIR}/%.o : %.c
	@echo "Build $(notdir $<)..."
	@-mkdir -p $(@D)
	@$(CC) $(CFLAGS) ${INCLUDE_DIRS} -MMD -c $< -o $@

execute:
	-cp ${BUILD_DIR}/${BIN} ./
	sudo ./${BIN}

clean:
	-rm -rf $(BUILD_DIR) ./${BIN}
